package my.mall.service.impl;

import my.mall.common.pojo.LoginUser;
import my.mall.common.pojo.vo.MenusVo;
import my.mall.entity.Permission;
import my.mall.entity.Role;
import my.mall.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Override
    public List<MenusVo> getMenus(LoginUser loginUser) {
//        String psIds = null;
//        if (loginUser.getRoleId() != 0) {
//            Role role = roleMapper.selectById(loginUser.getRoleId());
//            psIds = role.getPsIds();
//        }
//        List<Permission> permissionList = permissionMapper.listById(psIds);
//        Map<Integer, List<Permission>> pidMap = permissionList.stream().collect(Collectors.groupingBy(Permission::getPsPid));
//        List<Permission> parentList = pidMap.get(0);
//        if (parentList == null || parentList.size() == 0) {
//            return Collections.emptyList();
//        }
//        List<MenusVo> res = new ArrayList<>(parentList.size());
//        for (Permission permission : parentList) {
//            MenusVo menusVo = new MenusVo();
//            menusVo.setAuthName(permission.getPsName()).setOrder(permission.getPsId()).setPath(permission.getPsA());
//            res.add(menusVo);
//            List<Permission> childList = pidMap.get(permission.getPsId());
//            if (childList == null || childList.size() == 0) {
//                continue;
//            }
//            List<MenusVo> childRes = new ArrayList<>(childList.size());
//            for (Permission child : childList) {
//                MenusVo childMenusVo = new MenusVo();
//                childMenusVo.setAuthName(child.getPsName())
//                        .setOrder(child.getPsId()).setPath(child.getPsA());
//                childRes.add(childMenusVo);
//            }
//            menusVo.setChildren(childRes);
//        }
        return null;
    }
}
